package com.ruoyi.config


import cn.hutool.core.util.RandomUtil
import com.aliyun.dysmsapi20170525.models.SendSmsRequest
import com.aliyun.teaopenapi.models.Config
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.stereotype.Component

@Component
@EnableConfigurationProperties(AliSmsConfig::class)
class AliSms {

    @Autowired
    private lateinit var aliSmsConfig: AliSmsConfig

    private val log = LoggerFactory.getLogger(AliSms::class.java)


    fun send(mobile: String): String =
        RandomUtil.randomNumbers(6).also {
            com.aliyun.dysmsapi20170525.Client(Config().apply {
                accessKeyId = aliSmsConfig.id
                accessKeySecret = aliSmsConfig.key
                endpoint = aliSmsConfig.endPoint
            }).sendSms(SendSmsRequest().apply {
                signName = "研律"
                templateCode = "SMS_119092323"
                phoneNumbers = mobile
                templateParam = """
                {"code": $it}
            """.trimIndent()
            }).also { it.run {
                body.run {
                    log.info("发送短信的结果是$code:$message")
                }
            } }
        }
}